home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Music⁄Sounds / BeepSay 1.0.1 / SayIt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-22  |  5.0 KB  |  179 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2. **
  3. **  Folder Name:    BS
  4. **     File Name:    SayIt.c
  5. **
  6. **   Copyright:    © 1993 by Siren Enterprises, all rights reserved.
  7. **
  8. **   Description:    MPW Tool to send Apple Event to Lil´ BeepSay to speak a
  9. **                    given phrase
  10. **
  11. *******************************************************************************
  12. **                       A U T H O R   I D E N T I T Y
  13. *******************************************************************************
  14. **
  15. **    Initials    Name
  16. **    --------    -----------------------------------------------
  17. **    kw            Ken Wieschhoff
  18. **
  19. *******************************************************************************
  20. **                      R E V I S I O N   H I S T O R Y
  21. *******************************************************************************
  22. **
  23. **      Date        Time    Author    Description
  24. **    --------    -----    ------    ---------------------------------------------
  25. **    06/22/93    16:42    kw        1+ Check for Apple Events via Gestalt
  26. **    06/01/93    19:01    kw        Original version
  27. **
  28. ******************************************************************************/
  29.  
  30. #include    <ToolUtils.h>
  31. #include    <Fonts.h>
  32. #include    <Packages.h>
  33. #include    <Strings.h>
  34. #include    <Resources.h>
  35. #include    <AppleEvents.h>
  36. #include    <Processes.h>
  37. #include    "Utilities.h"
  38. #include     <stdio.h>
  39. #include     <CType.h>
  40. #include     <string.h>
  41. #include     <GestaltEqu.h>
  42. #include     <CursorCtl.h>
  43.  
  44. static    char    *usage = "# Usage - %s [-h] | [<phrase to speak>] \n";
  45.  
  46. pascal Boolean SpeakIdleProc(EventRecord *event,
  47.                     long *sleepTime, RgnHandle *mouseRgn);
  48.  
  49. main(int argc, char *argv[])
  50. {
  51.     ProcessSerialNumber itsNumber;
  52.     AEDesc                 theAddress;
  53.     AppleEvent             ourEvent,ourReply;
  54.     Str255                textToSend = { 0 };
  55.     Size                dataSize;
  56.     DescType            theDescType;
  57.     long                errorCode;
  58.     Str255                returnedText;
  59.     OSErr                err;
  60.     long                result;
  61.     long                parms;
  62.     long                    length;
  63.     
  64.     // Check for AppleEvents
  65.     if( Gestalt(gestaltAppleEventsAttr, &result) != noErr) {
  66.         fprintf(stdout, "### Apple Events are not supported on this machine!\n");
  67.         return 1;
  68.     }
  69.  
  70.     // Check if Lil´ BeepSay is even installed.
  71.     if (Gestalt( kBeepSaySignature, &result) != noErr) {
  72.         fprintf(stdout, "### Lil´ BeepSay is not installed!\n");
  73.         return 1;
  74.     }
  75.         
  76.     // Parse parameters
  77.     for (parms = 1; parms < argc; parms++) {
  78.         length = strlen(argv[parms]);
  79.         if (*argv[parms] != '-') {
  80.             strcat(textToSend, argv[parms]);
  81.         } 
  82.         // -h for help.
  83.         else if (tolower(*(argv[parms]+1)) == 'h' && length == 2) {
  84.             fprintf(stdout, usage, argv[0]);
  85.             return 0;
  86.         } 
  87.         else {
  88.             fprintf(stderr,"### %s - \"%s\" is not an option.\n", argv[0], argv[parms]);
  89.             fprintf(stderr, usage, argv[0]);
  90.             return 1;
  91.         }
  92.     }
  93.  
  94.     if ( strlen( textToSend) == 0) {
  95.         fprintf(stderr,"### Need a phrase to speak.\n");
  96.         fprintf(stderr, usage, argv[0]);
  97.         return 1;
  98.     }
  99.  
  100.  
  101.     // We create an Apple Event and send it to ourselves.  Since we don't have a 
  102.     // handler for this it will land in the system handler we've created
  103.     // in Lil´ BeepSay
  104.     itsNumber.highLongOfPSN = 0;
  105.     itsNumber.lowLongOfPSN = kCurrentProcess;
  106.     err =  AECreateDesc(typeProcessSerialNumber, (Ptr)&itsNumber, 
  107.                         sizeof(ProcessSerialNumber), &theAddress);
  108.  
  109.     if (err) {
  110.         fprintf(stdout,"###Could not create Apple Event Descriptor. Error = %d\n", err);
  111.         return 1;
  112.     }
  113.     
  114.     // Create the apple event
  115.     err =  AECreateAppleEvent(kBeepSaySignature, kBeepSaySpeakPhrase,  &theAddress, 
  116.                                 kAutoGenerateReturnID, kAnyTransactionID, &ourEvent);
  117.         
  118.     if (err) {
  119.         fprintf(stdout,"###Could not create Apple Event. Error = %d\n", err);
  120.         AEDisposeDesc( &ourEvent);    
  121.         AEDisposeDesc( &theAddress);
  122.         return 1;
  123.     }
  124.     
  125.     // Set the text buffer to use
  126.     err = AEPutParamPtr(&ourEvent, keyDirectObject, typeChar, 
  127.             (Ptr) textToSend, strlen(textToSend));
  128.         
  129.     if (err) {
  130.         fprintf(stdout,"###Could not add text to Apple Event. Error = %d\n", err);
  131.         AEDisposeDesc( &ourEvent);    
  132.         AEDisposeDesc( &theAddress);
  133.         return 1;
  134.     }
  135.     
  136.     // Send to ourselves.  Since we don't handle these,
  137.     // they'll be dispatched to the system handler in BeepSay
  138.     err =  AESend(&ourEvent, &ourReply, kAEWaitReply, 
  139.                 kAEHighPriority, kAEDefaultTimeout, (IdleProcPtr) &SpeakIdleProc, nil);
  140.  
  141.     if (err)
  142.         fprintf(stdout,"###Could not send Apple Event. Error = %d\n", err);
  143.     
  144.     // Check for an error number.
  145.     err = AEGetParamPtr(&ourReply, keyErrorNumber, typeLongInteger, 
  146.                 &theDescType, (Ptr)&errorCode, sizeof(long), 
  147.                     &dataSize);
  148.  
  149.     if (err && err != errAEDescNotFound)
  150.         fprintf(stdout,"###Error returned from Apple Event. Error = %d\n", err);
  151.  
  152.     if (!err)
  153.         fprintf(stdout,"###Error returned from Apple Event. Error = %d\n", err);
  154.  
  155.  
  156.     // try for the error string
  157.     err = AEGetParamPtr(&ourReply, keyErrorString, typeChar, &theDescType, 
  158.                 (Ptr) returnedText, 255, &dataSize);
  159.  
  160.     if (!err) {
  161.         returnedText[dataSize] = '\0'; // null terminate
  162.         fprintf(stdout,"###Error text = %s\n", returnedText);
  163.     }
  164.         
  165.     AEDisposeDesc( &ourEvent);
  166.             
  167.     AEDisposeDesc( &theAddress);
  168.     
  169.     return 0;
  170. }
  171.  
  172. pascal Boolean SpeakIdleProc(EventRecord *event,
  173.                     long *sleepTime, RgnHandle *mouseRgn)
  174. {
  175. #pragma unused (event, sleepTime, mouseRgn)
  176.     SpinCursor(1);
  177.     return false;
  178. }
  179.